package com.atguigu.juc.locks;

import java.util.concurrent.TimeUnit;

/**
 * 两个静态同步方法，一部手机，请问先打印邮件还是短信？
 * @author yfstart
 * @create 2022-06-28 11:31
 */
public class Lock8Demo5 {
  public static void main(String[] args) {
      Phone5 phone = new Phone5(); //this,当前实例对象

      new Thread(() -> {
          phone.sendEmail();
      }, "A").start();

      // 保证线程A在线程B之前执行
      try { TimeUnit.MILLISECONDS.sleep(500); } catch (InterruptedException e) { e.printStackTrace();}

      new Thread(() -> {
          phone.sendSMS();
      }, "B").start();
  }
}

/**
 * 资源类  Phone.java---Phone.class(字节码文件小class)---加载进入JVM形成模板大Class Phone.Class
 */
class Phone5 {
    public static synchronized void sendEmail() {
        try { TimeUnit.MILLISECONDS.sleep(3000); } catch (InterruptedException e) { e.printStackTrace();}
        System.out.println("-----Email");
    }

    public static synchronized void sendSMS() {
        System.out.println("-----SMS");
    }
}
